package com.iflytek.iot.redis.sharding.core;


import com.iflytek.iot.redis.sharding.core.constants.RedisShardingConstants;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author ZuRun
 * @date 2020/2/26 11:35:11
 */
public class RedisClientPool<T> extends AbstractRedisClientPool<T> implements IRedisClientPool<T> {

    protected RedisClientPool() {
    }

    public T getResource(String slotName) {
        return getResource(RedisShardingConstants.DEFAULT_REDIS_GROUP, slotName);
    }

    public T getResource(IRedisGroup redisGroup, String slotName) {
        return getResource(redisGroup.getGroupName(), slotName);
    }

    @Override
    public List<T> getRedisClients(IRedisGroup group) {
        return getResources(group.getGroupName());
    }

    @Override
    public T getRedisClient(String key) {
        return getRedisClient(RedisShardingConstants.DEFAULT_REDIS_GROUP, key);
    }

    @Override
    public T getRedisClient(IRedisGroup group, String key) {
        return getResourceByKey(group.getGroupName(), key);
    }

    @Override
    public void execute(String key, Consumer<T> resourceConsumer) {
        T redisClient = getRedisClient(key);
        resourceConsumer.accept(redisClient);
    }

    @Override
    public void execute(IRedisGroup group, String key, Consumer<T> resourceConsumer) {
        T redisClient = getRedisClient(group, key);
        resourceConsumer.accept(redisClient);
    }

    @Override
    public <R> R execute(String key, Function<T, R> function) {
        T redisClient = getRedisClient(key);
        return function.apply(redisClient);
    }

    @Override
    public <R> R execute(IRedisGroup group, String key, Function<T, R> function) {
        T redisClient = getRedisClient(group, key);
        return function.apply(redisClient);
    }


}
